package com.tchintech.pay.view.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.TixianDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.AppConst;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/2 0002.
 */

public class TixianActivity extends ActivityPresenter<TixianDelegate> implements View.OnClickListener {
    private Map<String, String> map;
    private String amount;
    private Handler handler;
    private String tx_url = "Money.withdrawal";
    private String name_url = "Money.clear";
    private EditText et_je;
    private TextView tv_ye;
    private String usableM;
    private TextView tv_bankName, tv_wh;


    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setTv_title("提现");
        viewDelegate.setOnClickListener(this, R.id.iv_back, R.id.bt_tx, R.id.real_chk);
        et_je = viewDelegate.get(R.id.edit_money);
        tv_ye = viewDelegate.get(R.id.tv_ye);
        tv_bankName = viewDelegate.get(R.id.tv_bankName);
        tv_wh = viewDelegate.get(R.id.tv_wh);
        tv_ye.setText(usableM + "元");
        handler();
        query();
    }

    @Override
    protected void initData() {
        super.initData();
        usableM = getIntent().getStringExtra("usableM");
    }

    /**
     * 查询当前人的结算卡
     */
    private void query() {
        map = new HashMap<String, String>();
        map.put("user_id", AppConst.getUser().getId());
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, name_url, map, HttpCode.CLEAR_CARD);


    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.USER_TX:
                        try {
                            JSONObject jsono = new JSONObject(msg.obj.toString());
                            if (jsono.getString("ret").equals("1")) {
                                showMsg("提款成功");
                            } else {
                                showMsg(jsono.getString("data"));
                            }
                        } catch (Exception e) {
                        }
                        break;
                    case HttpCode.CLEAR_CARD:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                setBankname(json.getJSONObject("data"));
                            } else {
                                showMsg(json.getString("data"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                        break;
                }
            }
        };
    }

    private void setBankname(JSONObject json) {
        try {
            tv_bankName.setText(json.getString("bank_name"));
            String bankNum = json.getString("bank_number");
            String last_bankNum = bankNum.substring(bankNum.length() - 4, bankNum.length());
            tv_wh.setText("尾号为" + last_bankNum + "储蓄卡");
        } catch (Exception e) {

        }

    }


    private void submit() {
        map = new HashMap<String, String>();
        map.put("userid", AppConst.getUser().getId());
        map.put("amt", amount);
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, tx_url, map, HttpCode.USER_TX);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_tx:
                //提现功能
                amount = et_je.getText().toString();
                if (amount.equals("")) {
                    amount = "0";
                }
                double db = Double.parseDouble(amount);
                if (db >= 100.00) {
                    et_je.setText("");
                    submit();
                } else {
                    showMsg("提款金额需大于100");
                }
                break;
            case R.id.real_chk:
                //选择储蓄卡
                break;
            case R.id.iv_back:
                finish();
                break;
        }

    }

    @Override
    protected Class<TixianDelegate> getDelegateClass() {
        return TixianDelegate.class;
    }
}
